var util = require('../../libs/util.js');
var app = getApp();
Page({
  data: {
    step: 1,
    img_driving_licence: "/images/upload-add.svg",
    img_id_card_front: "/images/upload-add.svg",
    img_id_card_back: "/images/upload-add.svg",
    img_id_card_Handheld: "/images/upload-add.svg",
    img_driving_qualification: "/images/upload-add.svg",
    name: "",
    id_card: "",
    license_number: "",
    CO_NAME: '',
    CO_ID: '',
    enterpriseList: [],
    _enterpriseList: [],
  },
  onShareAppMessage: function () {
    return {
      title: app.appName,
      path: '/pages/index/index'
    }
  },
  onLoad: function (e) {
    var that = this;
    wx.showToast({
      title: "页面加载中\n请稍候...",
      icon: "loading",
      duration: 5000
    });
    util.getData('/wechat/passport/getCompany', {}, function (res) {
      console.log(res.data.data)
      if (res.data.success) {
        that.setData({
          enterpriseList: res.data.data
        })
      }
    });
  },
  onReady: function () {
    wx.hideToast()
  }, 
  clearCompany: function () {
    this.setData({
      CO_NAME: '',
      CO_ID: ''
    })
  },
  bindInput: function (e) {
    var item = String(e.currentTarget.id), item_value = {};
    item_value[item] = e.detail.value;
    this.setData(item_value);
  },
  enterpriseInput: function (e) {
    var that = this;
    var value = e.detail.value;
    var enterpriseList = that.data.enterpriseList;
    var _enterpriseList = [];
    for (var i = 0; i < enterpriseList.length; i++) {
      if (enterpriseList[i].CO_NAME.indexOf(value) > -1) {
        _enterpriseList.push(enterpriseList[i]);
      }
    };
    if (value == '') {
      _enterpriseList = [];
    }
    that.setData({
      _enterpriseList: _enterpriseList
    })
  },
  selectEnterprise: function (e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    var _enterpriseList = that.data._enterpriseList;
    for (var i = 0; i < _enterpriseList.length; i++) {
      if (_enterpriseList[i].CO_ID == id) {
        that.setData({
          CO_NAME: _enterpriseList[i].CO_NAME,
          CO_ID: _enterpriseList[i].CO_ID
        })
        that.hideModal();
        return false;
      }
    };
  },
  showModal(e) {
    this.setData({
      modalName: e.currentTarget.dataset.target
    })
  },
  hideModal(e) {
    this.setData({
      modalName: null
    })
  },
  chooseImage: function (e) {
    var that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        wx.showLoading({
          title: '上传中...',
          mask: true
        })

        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var item = String(e.currentTarget.id), item_value = {};
        item_value[item] = res.tempFilePaths[0];
        that.setData(item_value);
        wx.uploadFile({
          url: app.server + '/wechat/passport/uploadCard_user',
          header:{
            'Authorization': app.globalData.token
          },
          filePath: res.tempFilePaths[0],
          name: 'file',
          formData: {
            'CO_ID': app.globalData.CO_ID,
            'user_id': app.globalData.user_id,
            'id_card': that.data.id_card,
            'item': item
          },
          success: function (res) {
            var data = JSON.parse(res.data);
            console.log(data)
            if (data.success) {
              if (item == 'img_id_card_front') {
                that.setData({
                  'name': data.name,
                  'id_card': data.id_card_number
                });
              }
              var upload_item = String(e.currentTarget.id), item_value = {};
              var path = data.data[0].path;
              if (path.indexOf("http") == -1) {
                path = app.server + path
              }
              item_value[upload_item] = path;
              that.setData(item_value);
            } else {
              wx.showModal({
                content: data.data,
                confirmText: "确定",
                showCancel: false
              })
            }
          },
          complete: function () {
            wx.hideLoading()
          }
        })
      }
    })
  },
  previewImage: function (e) {
    wx.previewImage({
      current: e.currentTarget.id, // 当前显示图片的http链接
      urls: this.data.files // 需要预览的图片http链接列表
    })
  },
  submitForms: function (e) {
    var that = this;
    if (that.data.id_card == '' || that.data.id_card == undefined) {
      wx.showToast({
        title: '请上传身份证',
        image: '/images/wrong.png',
        duration: 3000,
        mask: true
      });
      return false;
    }
    if (that.data.CO_ID == '') {
      wx.showToast({
        title: '请选择所属企业',
        image: '/images/wrong.png',
        duration: 3000,
        mask: true
      });
      return false;
    }
    wx.showLoading({
      title: '请稍候...',
    });
    
    util.putData("/wechat/passport/managerReg", {
      CO_ID: that.data.CO_ID,
      user_id: app.globalData.user_id
    }, function (res, error) {
      console.log(res); console.log(error)
      wx.hideLoading();
      if (error) {
        wx.showToast({
          title: res.data.data,
          image: '/images/wrong.png',
          duration: 3000
        });
        return false;
      }
      if (res.data.success) {
        wx.showModal({
          title: '温馨提示',
          content: '恭喜你，注册成功！请联系你所在企业的管理员开通管理功能',
          success: function (res) {
            wx.redirectTo({
              url: '/pages/index/index?reload=true'
            })
          }
        })
      } else if (res.data.success == false) {
        wx.showToast({
          title: res.data.data,
          image: '/images/wrong.png',
          duration: 3000,
          mask: true
        });
      }
    });
  }
});